<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git合并监控工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
        }
        .form-container {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .form-group {
            flex: 1;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        table th:nth-child(1),
        table td:nth-child(1) {
            width: 20%;
        }
        
        table th:nth-child(2),
        table td:nth-child(2) {
            width: 40%;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Git合并监控工具</h1>
        
        <h2>添加监控项</h2>
        <div class="form-container">
            <div class="form-group">
                <label for="name">监控名称</label>
                <input type="text" id="name" placeholder="例如: 重要功能开发">
            </div>
            <div class="form-group" style="flex: 2;">
                <label for="path">Git目录路径</label>
                <input type="text" id="path" placeholder="例如: C:\projects\my-repo" style="width: 100%;">
            </div>
            <div class="form-group">
                <label for="branch">分支名称</label>
                <input type="text" id="branch" placeholder="例如: feature-branch">
            </div>
        </div>
        <button id="addBtn">添加监控</button>
        
        <div class="monitor-section">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <h2>当前监控项</h2>
                <button class="refresh-btn" onclick="loadMonitors()">手动刷新</button>
            </div>
            <table id="monitorsTable">
                <thead>
                    <tr>
                        <th>监控名称</th>
                        <th>Git目录</th>
                        <th>分支名称</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 监控项将在这里动态添加 -->
                </tbody>
            </table>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const addBtn = document.getElementById('addBtn')
            const monitorsTable = document.getElementById('monitorsTable').getElementsByTagName('tbody')[0]
            
            // 加载现有监控项
            loadMonitors()
            
            // 添加监控项
            addBtn.addEventListener('click', () => {
                const path = document.getElementById('path').value
                const branch = document.getElementById('branch').value
                const name = document.getElementById('name').value
                
                if (!path || !branch || !name) {
                    alert('请填写所有字段')
                    return
                }
                
                fetch('/api/monitors', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ path, branch, name })
                })
                .then(() => {
                    document.getElementById('path').value = ''
                    document.getElementById('branch').value = ''
                    document.getElementById('name').value = ''
                    loadMonitors()
                })
                .catch(err => console.error('添加监控项失败:', err))
            })
            
            // 删除监控项
            monitorsTable.addEventListener('click', (e) => {
                if (e.target.classList.contains('delete-btn')) {
                    const row = e.target.closest('tr')
                    const path = row.dataset.path
                    const branch = row.dataset.branch
                    const name = row.dataset.name
                    
                    fetch('/api/monitors', {
                        method: 'DELETE',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ path, branch, name })
                    })
                    .then(() => loadMonitors())
                    .catch(err => console.error('删除监控项失败:', err))
                }
            })
            
            // 加载监控项列表
            function loadMonitors() {
                console.log('开始加载监控项...');
                fetch('/api/monitors')
                    .then(res => {
                        console.log('API响应状态:', res.status);
                        return res.json();
                    })
                    .then(monitors => {
                        console.log('接收到的监控项数据:', monitors);
                        // 不再需要动态添加刷新按钮
            
            monitorsTable.innerHTML = ''
            
            monitors.forEach(monitor => {
                const row = document.createElement('tr')
                row.dataset.path = monitor.path
                row.dataset.branch = monitor.branch
                row.dataset.name = monitor.name
                
                row.innerHTML = `
                    <td>${monitor.name}</td>
                    <td>${monitor.path}</td>
                    <td>${monitor.branch}</td>
                    <td><button class="delete-btn">删除</button></td>
                `
                            
                            monitorsTable.appendChild(row)
                        })
                    })
                    .catch(err => console.error('加载监控项失败:', err))
            }
            window.loadMonitors = loadMonitors // 暴露loadMonitors函数给页面上的JavaScript脚本使用
        })
    </script>
</body>
</html>